/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package medium;
import java.net.*;
import java.io.*;
import eac.view.*;
import library.view.DeliveryDesk;
import medium.model.SocketModel;

/*
 *
 * @author Donghui
 */

public class MsgSender {

    private PrintWriter sender;
    private BufferedReader reader;
    private Socket socket;
    private static MsgSender local;

    public MsgSender()
    {
        try{
        byte[] address=new byte[]{(byte)192,(byte)168,(byte)222,101}; // the scope of byte type is -128~127, so there is no need to add byte mark to 103
        InetAddress ip=InetAddress.getByAddress(address);
        socket=new Socket(ip, 9000);
        sender=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        MsgReader.getInstance(reader, sender).start();
//        local=this;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static MsgSender getInstance()
    {
        if(local==null)
        {
            local=new MsgSender();
        }
        return local;
    }

    public void toEACShowAchievementInfo()
    {
        try{
        String id=EACenter.getInstance().getValueModel().getId();
        String msg=id+";01";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toAddNewAchievement(String tp)
    {
        try{
        String msg= tp+";02";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toAddNewCourse(String tp)
    {
        try{
        String msg= tp+";03";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toVerify(String tpid, String tppsw)
    {
        try{
//        String id=MainTable.getInstance().getIDModel().getUserID();
//        String psw=PswTable.getInstance().getUserPsw_Model().getPsw();

        String id=tpid;
        String psw=tppsw;
        String msg=id+";"+psw+";04";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toDeleteAchievement(String tp)
    {
        try{
        String msg= tp+";05";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toEACShowCourseInfo()
    {
        try{
        String id=EACenter.getInstance().getValueModel().getTid();
        String msg=id+";06";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toDeleteCourse(String tp)
    {
        try{
        String msg= tp+";07";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toLibVerify(String tpid, String tppsw)
    {
        try{
        String id=tpid;
        String psw=tppsw;
        String msg=id+";"+psw+";08";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toLibShowBook()
    {
        try{
        String bid=DeliveryDesk.getInstance().getValueModel().getBid();
        String msg=bid+";09";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toNewBorrower(String tp)
    {
        try{
        String msg=tp+";10";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toUpdateCard(String tp)
    {
        try{
        String msg=tp+";11";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void toExit()
    {
        try{
        String msg="exit"+";99";
        sender.println(msg);
        System.out.println(msg);
        sender.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public SocketModel getSocketModel()
    {
        SocketModel model=new SocketModel();
        model.setSocket(socket);
        model.setReader(reader);
        model.setSender(sender);
        return model;
    }
}